package com.jd.org.controller;

import com.jd.basic.utils.ExcelUtil;
import com.jd.org.domain.Shop;
import com.jd.org.domain.ShopAuditLog;
import com.jd.org.dto.ShopSettlementAgainDto;
import com.jd.org.dto.ShopSettlementDto;
import com.jd.org.query.ShopQuery;
import com.jd.basic.utils.AjaxResult;
import com.jd.basic.utils.PageList;
import com.jd.org.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    private ShopService shopService;


    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.add(shop);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return AjaxResult.me().success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id") Long id)
    {
        return shopService.findById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Shop> list(){

        return shopService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Shop> json(@RequestBody ShopQuery query)
    {
        return shopService.queryByPage(query);
    }

    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody @Validated ShopSettlementDto shopSettlementDto){
        try {
            shopService.settlement(shopSettlementDto);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail().setMsg(e.getMessage());
        }
    }


    @PostMapping("/audit/pass")
    public AjaxResult sendAudit(@RequestBody @Validated ShopAuditLog shopAuditLog){
        try {
            shopService.sendAudit(shopAuditLog);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }

    @PostMapping("/audit/reject")
    public AjaxResult sendReject(@RequestBody  @Validated ShopAuditLog shopAuditLog){
        try {
            shopService.sendReject(shopAuditLog);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }

    @GetMapping("/export")
    public void export(HttpServletResponse response){
        List<Shop> shops = shopService.findAll();
        ExcelUtil.exportExcel(shops,"店铺列表","",Shop.class,"店铺列表.xslx",response);
    }

    @PostMapping("/import")
    public void export(@RequestParam(value = "file",required = true ) MultipartFile file){
        List<Shop> shops = ExcelUtil.importExcel(file, 1, 1, Shop.class);

    }

    @GetMapping("/registerAgain/{id}")
    public Shop getregister(@PathVariable("id") Long id)
    {
        return shopService.findById(id);
    }

    @PostMapping("/settlementAgain")
    public AjaxResult settlementAgain(@RequestBody Shop shop ){
        try {
            shopService.settlementAgain(shop);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail().setMsg(e.getMessage());
        }
    }

    @GetMapping("/echarts")
    public Map<String,Object> echarts(){
        return shopService.echarts();
    }

}
